package com.sxz.sbs.module.staff;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.pickerview.OptionsPickerView;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sxz.sbs.R;
import com.sxz.sbs.applibrary.component.ClearEditText;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.staff.mode.DepartmentListBean;
import com.sxz.sbs.module.staff.mode.PositionListBean;
import com.sxz.sbs.module.staff.presenter.AddStaffPresenter;
import com.sxz.sbs.module.staff.view.AddStaffView;
import com.sxz.sbs.router.RouterUrl;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 添加员工账号
 */
@Route(path = RouterUrl.ADD_MANAGER)
public class AddStaffActivity extends BaseActivity<AddStaffPresenter> implements AddStaffView {


    @BindView(R.id.cet_add_staff_name)
    ClearEditText cetAddStaffName;
    @BindView(R.id.cet_add_staff_phone)
    ClearEditText cetAddStaffPhone;
    @BindView(R.id.tv_add_staff_department)
    TextView tvAddStaffDepartment;
    @BindView(R.id.tv_add_staff_job)
    TextView tvAddStaffJob;
    @BindView(R.id.tv_add_staff_parking)
    TextView tvAddStaffParking;
    private OptionsPickerView departmentPicker, jobPicker;
    private List<DepartmentListBean> departmentData = new ArrayList<>();
    private List<PositionListBean> jobData = new ArrayList<>();
    private int selectDepartId, selectPositionId, selectParkingId;

    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle(getString(R.string.staff_title)).build().show();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_staff;
    }

    @Override
    protected AddStaffPresenter createPresenter() {
        return new AddStaffPresenter();
    }

    @OnClick({R.id.tv_add_staff_department, R.id.tv_add_staff_job, R.id.btn_add_staff, R.id.tv_add_staff_parking})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add_staff_department:
                if (departmentData.size() > 0) {
                    if (departmentPicker != null) {
                        departmentPicker.show();
                    }
                } else {
                    mPresenter.getDepartmentList(HttpParamMap.getParamMap());
                }
                break;
            case R.id.tv_add_staff_job:
                if (TextUtils.isEmpty(tvAddStaffDepartment.getText().toString())) {
                    ToastUtils.showShort("请先选择部门");
                    return;
                }
                HashMap<String, Object> map = HttpParamMap.getParamMap();
                map.put("departmentId", selectDepartId);
                mPresenter.getPositionList(map);
                break;
            case R.id.tv_add_staff_parking:
                ARouter.getInstance().build(RouterUrl.STAFF_HANDLE)
                        .withString("status", "status=1234")
                        .navigation(this, 1005);
                break;
            case R.id.btn_add_staff:
                String name = cetAddStaffName.getInputValue();
                if (TextUtils.isEmpty(name)) {
                    cetAddStaffName.setShakeAnimation();
                    return;
                }
                String phone = cetAddStaffPhone.getInputValue();
                if (!RegexUtils.isMobileExact(phone)) {
                    ToastUtils.showShort("请输入正确的手机号");
                    return;
                }
                if (TextUtils.isEmpty(tvAddStaffDepartment.getText().toString())) {
                    ToastUtils.showShort("请选择部门");
                    return;
                }
                if (TextUtils.isEmpty(tvAddStaffJob.getText().toString())) {
                    ToastUtils.showShort("请选择职位");
                    return;
                }
                if (TextUtils.isEmpty(tvAddStaffParking.getText().toString())) {
                    ToastUtils.showShort("请选择案场");
                    return;
                }
                HashMap<String, Object> map1 = HttpParamMap.getParamMap();
                map1.put("m.txt_Name", name);
                map1.put("m.txt_Phone", phone);
                map1.put("m.Department_Id", selectDepartId);
                map1.put("m.Manager_Position_Id", selectPositionId);
                map1.put("parkingId", selectParkingId);
                mPresenter.addStaff(map1);
                break;
            default:
                break;
        }
    }

    /**
     * 初始化部门弹窗
     */
    private void initDepartmentPicker() {
        departmentPicker = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (tvAddStaffDepartment.getText().toString().equals(departmentData.get(options1).getTxt_DepartmentName())) {
                    return;
                }
                selectPositionId = 0;
                tvAddStaffJob.setText("");
                tvAddStaffDepartment.setText(departmentData.get(options1).getTxt_DepartmentName());
                selectDepartId = departmentData.get(options1).getId();
            }
        }).build();
        departmentPicker.setPicker(departmentData);
        departmentPicker.show();
    }

    /**
     * 初始化部门弹窗
     */
    private void initJobPicker() {
        jobPicker = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

                tvAddStaffJob.setText(jobData.get(options1).getTxt_PositionName());
                selectPositionId = jobData.get(options1).getId();
            }
        }).build();
        jobPicker.setPicker(jobData);
        jobPicker.show();
    }

    @Override
    public void onGetDepartmentListSuccess(List<DepartmentListBean> data) {
        if (mIsLive && data != null) {
            if (data.size() > 0) {
                departmentData.addAll(data);
                if (departmentPicker != null) {
                    departmentPicker.show();
                } else {
                    initDepartmentPicker();
                }
            } else {
                ToastUtils.showShort("无部门数据");
            }
        }
    }

    @Override
    public void onGetPositionListSuccess(List<PositionListBean> data) {
        if (mIsLive && data != null) {
            if (data.size() > 0) {
                jobData.clear();
                jobData.addAll(data);
                initJobPicker();
            } else {
                ToastUtils.showShort("无对应数据");
            }

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        switch (requestCode) {
            case 1005:
                selectParkingId = data.getIntExtra("parkId", 0);
                tvAddStaffParking.setText(data.getStringExtra("parkName"));
                break;
            default:
                break;
        }
    }

    @Override
    public void onAddStaffSuccess() {
        ToastUtils.showShort("添加成功");
        finish();
    }
}
